﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading;
using Net.Fly.Article.Util;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;

namespace Net.Fly.Article.AutoSubmit
{
    class PubarticlesSubmit : AbstractSubmit
    {
        private String m_rootURL = "http://www.pubarticles.com";
        private String m_memberURL = "http://goarticles.com/members/";
        private String m_userName = "flying_book@163.com";
        private String m_password = "bright820813";
        private String m_loginURL = "http://www.pubarticles.com/members/login.php";
        private String m_submitURL = "http://www.pubarticles.com/members/articles_submit_post.php";
        private String m_mainCatagory = "business";
        private String m_subCatagory = "Information Technology";
        private String m_title = "Use Free Article Spinner Help Article Write";
        private String m_article = "You can easily to use it online, no any extra paid, the only thing you need to do is register on Home Page, then you can have fun.";
        private String m_summary = "All of the webmasters know how to increase the traffic of the website by writing articles, but the jobs are really boring, they hope there are tools can help them to finish the task efficient. What are the tools? For sure, article spinner is the famous one, sometimes we call it as article re-writer. It can replace the words or phrases in the article with synonymous words, so you can utilize this tool to generate hundreds of unique articles based on one article template. Amazing, right? But lots of these tools are not free,and the payment is really high. And the bad thing is most of tools are desktop applications, after you paid, installed, used, found it is not very useful. What I would like to suggest is using free article spinner online version, save money, get synonymous words from other users, and other benefit. Today I recommend one free article spinner - articlewritebee.com, this tool developed by Tom who is a expert on programming, and very familiar with the technology on writing.";
        private String m_bio = "Our site focus on providing NO1 services on Article Market, our free article spinner can help article writer to generate unique article in minutes, and users don’t need purchase articles any more because we provide spinner articles as well. In the end, you can use our submitter to publish your article in thousands of article directories. Please visit <a href=www.articlewritebee.com/index.php>Free";
        private String m_author = "Bright";
        private String m_errMessage = "";
        private String[] m_keywords = { "article submission", "backlink", "SEO" };

        private SubmitStatus m_status = SubmitStatus.NEW;

        private IWebDriver m_driver;


        public PubarticlesSubmit()
        {
            m_driver = new FirefoxDriver();
        }

        public override bool Login()
        {
            m_status = SubmitStatus.LOGINING;

            try
            {
                m_driver.Navigate().GoToUrl(m_loginURL);

                IWebElement userInput = WebDriverExtension.FindElement(m_driver, By.Id("email"), 5);// m_driver.FindElement(By.XPath("//label/input[@name='email']"));
                userInput.SendKeys(m_userName);

                IWebElement passwordInput = WebDriverExtension.FindElement(m_driver, By.Id("password"), 5);  // m_driver.FindElement(By.XPath("//label/input[@name='password']"));
                passwordInput.SendKeys(m_password);

                IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.Id("login_btn"), 5);  //m_driver.FindElement(By.XPath("//form/input[@value='Login']"));
                submitButton.Click();
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
                return false;
            }
            
            return true;
        }

        public override bool Submit()
        {
            m_status = SubmitStatus.SUBMITTING;
            try
            {
                m_driver.Navigate().GoToUrl(m_submitURL);

                // Select main Catagory
                IWebElement catagoryMain = WebDriverExtension.FindElement(m_driver, By.XPath("//label/input[@value='" + m_mainCatagory + "']"), 5);
                catagoryMain.Click();

                // Set title
                IWebElement titleInput = WebDriverExtension.FindElement(m_driver, By.XPath("//div/input[@class='ml12']"), 5);
                titleInput.SendKeys(m_title);

                // Set Tags
                if (m_keywords != null && m_keywords.Length >= 1)
                {
                    IWebElement tag1Input = WebDriverExtension.FindElement(m_driver, By.Id("tag1"), 5);
                    tag1Input.SendKeys(m_keywords[0]);

                    if (m_keywords.Length >= 2)
                    {
                        IWebElement tag2Input = WebDriverExtension.FindElement(m_driver, By.Id("tag2"), 5);
                        tag2Input.SendKeys(m_keywords[1]);
                    }

                    if (m_keywords.Length >= 3)
                    {
                        IWebElement tag3Input = WebDriverExtension.FindElement(m_driver, By.Id("tag3"), 5);
                        tag3Input.SendKeys(m_keywords[2]);
                    }
                }

                IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.XPath("//input[@class='form_area_submit_btn1 form_area_btn_okey ml194 mb10 mt25']"), 5);

                // switch to iframe
                m_driver.SwitchTo().Frame("baidu_editor_0");

                // Set introduction
                IWebElement introInput = WebDriverExtension.FindElement(m_driver, By.Id("introduction"), 5);
                IWebElement intro = introInput.FindElement(By.XPath("//p/br"));
                intro.SendKeys(m_summary);

                // Set article body
                IWebElement bodyInput = WebDriverExtension.FindElement(m_driver, By.XPath("//div[@id='steps']/ol/li/p/br"), 5);
                bodyInput.SendKeys(m_article);

                // Set bio
                IWebElement bioInput = WebDriverExtension.FindElement(m_driver, By.XPath("//div[@id='related_resources']/p/br"), 5);
                bioInput.SendKeys(m_bio);

                // Set author name
             //   IWebElement authorInput = WebDriverExtension.FindElement(m_driver, By.XPath("//label/input[@name='author_name']"), 5);
              //  authorInput.Clear();
             //   authorInput.SendKeys(m_author);

                

                // Submit
                
                submitButton.Click();


                // check error message
                IWebElement errBox = WebDriverExtension.FindElement(m_driver, By.Id("errorBox"), 5);
                if (errBox != null)
                {

                    m_errMessage = errBox.Text;
                    m_status = SubmitStatus.ERROR;
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
                return false;
            }

            m_status = SubmitStatus.SUCCESS;
            return true;
        }

        public override String GetArticleURL()
        {
            if (m_status != SubmitStatus.SUCCESS)
            {
                return "";
            }

            try
            {
                m_driver.Navigate().GoToUrl(m_memberURL);

                // find new article
                ReadOnlyCollection<IWebElement> hrefs = WebDriverExtension.FindElements(m_driver, By.XPath("//a[@target='newarticle']"), 5);
                foreach (IWebElement ele in hrefs)
                {
                    if (ele.Text.Contains(m_title))
                        return ele.GetAttribute("href");
                }
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
            }

            return "";
        }
    }
}
